/**
 * @version 0.0.4
 * 
 * @fileoverview
 *   Entrypoint to UXperf library.
 *
 * @example
 * 
 * <script type="text/javascript" src="uxperf.js"></script>
 * <script type="text/javascript">
 * uxperf.init({
 * 	'target': {
 * 		'type': 'remote',
 * 		'method': 'POST',
 * 		'url': 'http://localhost:8080/save',
 * 		'key': '21:38_72h3n*%xh218wdxuw+xhd8uxh82husdx#$hn8uhdxn21',
 * 		'key_in_hash': true,
 * 		'domain_in_hash': true,
 * 		'timeout': 10000
 * 	},
 * 	'storage': {
 * 		'prefix': 'uxperf',
 * 		'type': 'local'
 * 	},
 * 	'queue': {
 * 		'auto_delay': 1000,
 * 		'count': 10,
 * 		'onunload': false,
 * 		'autoinit' : 'deferred',
 * 		'delay' : 1000
 * 	},
 * 	'sensors': [
 * 		{
 * 			'type' : 'page_performance',
 * 			'custom' : session_id ? session_id : undefined,
 * 			'autoinit' : 'deferred',
 * 			'autoinit_delay' : 1
 * 		},
 * 		{
 * 			'type' : 'browser_event',
 * 			'custom' : session_id ? session_id : undefined,
 * 			'event_type' : [
 * 				'click',
 * 				{
 * 					'event_type': 'scroll',
 * 					'event_target': document.body,
 * 					'capture': false
 * 				}
 * 			],
 * 			'event_target' : 'mainform',
 * 			'capture' : true,
 * 			'autoinit' : 'deferred',
 * 			'delay' : 1000
 * 		},
 * 		{
 * 			'type' : 'browser_event',
 * 			'custom' : session_id ? session_id : undefined,
 * 			'event_type' : {
 * 				'focus': 'mainform',
 * 				'blur': {
 * 					'event_target' : 'mainform',
 * 					'capture' : false
 * 				}
 * 			},
 * 			'event_target' : document.body,
 * 			'capture' : true,
 * 			'autoinit' : 'deferred',
 * 			'delay' : 1000
 * 		},
 * 		{
 * 			'type' : 'ajax',
 * 			'trace_headers': true,
 * 			'with_url_path': false,
 *			'use_url_path': false,
 *			'with_data': ['param1','param2'],
 *			'with_params' : [
 *				'_flowId',
 *				'_action'
 * 			],
 *			'with_response': false,
 *			'with_status_text': false,
 *			'ext_req_id': function () { return this.params_['_flowId'] || '?'; },
 *			'event_type': [
 *				'success',
 *				'abort',
 *				'error',
 *				'error_upload',
 *				'timeout',
 *				'timeout_upload',
 *				'complete',
 *				'before_send'
 *			]
 * 		}
 * 	],
 * 	//'config_preserve': null, //there is plan in next versions to provide 'localStorage' option to save configuration in local storage (it is reasonable if there is one entrypoint to application and you need uxperf on many subpages)
 * ' ': ' ' //just to skip removing commas in previous values
 * });
 * </script>
 *
 * @author Sebastian Brzuzek sebastian@brzuzek.pl
 */

goog.provide('uxperf');
goog.provide('uxperf.init');

goog.require('uxperf.Config');
goog.require('uxperf.profiler.Profiler');
goog.require('uxperf.Storage');
goog.require('uxperf.utils');
goog.require('goog.debug.Logger');
goog.require('goog.debug.Console');

if (goog.DEBUG) {
  goog.provide('LOG');
  /**
   * @type {goog.debug.Logger}
   */
  LOG = null;
}

/**
 * main initialization function
 *
 * @param {Object} cfg configuration structure
 * @param {Function=} callback_opt optional function to call after initialization
 */
uxperf.init = function(cfg, callback_opt) {
  try {
    uxperf.Config.set(cfg || {});
    if (uxperf.Storage.getInstance()) {
      LOG = goog.debug.Logger.getLogger(uxperf.Storage.getPrefix());
      goog.debug.Console.autoInstall();
      goog.debug.Console.instance.setCapturing(true);
      if (goog.DEBUG) LOG.info('Storage ready');
      uxperf.profiler.Profiler.getInstance(callback_opt);
      if (goog.DEBUG) LOG.info('Profiler created');
    }
    if (goog.DEBUG) {
      if (window.console) {
        window.console.log('Storage: ', uxperf.Config.get('storage'));
        window.console.log('Queue: ', uxperf.Config.get('queue'));
        window.console.log('Target: ', uxperf.Config.get('target'));
        window.console.log('Sensors: ', uxperf.Config.get('sensors'));
      }
    }
  } catch (e) {
    if (goog.DEBUG) {
      try {
        goog.debug.Logger.getLogger('uxperf').info('Error ', e);
      } catch (ee) { }
    } else {
      if (window.console) window.console.log('Error: ' + e);
    }
  }
};

/**
 * total cleanup of storage for reset of library
 */
uxperf.cleanup = function() {
	uxperf.Storage.cleanup();
};

/**
 * Exports of symbols for external use
 */
goog.exportSymbol('uxperf.init', uxperf.init);
goog.exportSymbol('uxperf.cleanup', uxperf.cleanup);

//put additional exports here if you need it
//goog.exportSymbol('uxperf.Config', uxperf.Config);
//goog.exportSymbol('uxperf.Config.set', uxperf.Config.set);
//goog.exportSymbol('uxperf.Config.get', uxperf.Config.get);
